/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import org.eclipse.jface.viewers.TreeNode;
import org.leeloo.core.Tag;
import org.leeloo.core.Tags;
import org.leeloo.databinding.ui.test.support.PluginTestCase;
import org.leeloo.internal.core.ObjectDescriptor;

public class LabeledTreeNodeBuilderTest extends PluginTestCase {
    private LabeledTreeNode parent;
    private LabeledTreeNodeBuilder builder;
    
    protected void setUp() throws Exception {
        parent = new LabeledTreeNode("");
        builder = LabeledTreeNodeBuilder.forNode(parent);
    }
    
    public void testAddObjectDescriptor() throws Exception {
        ObjectDescriptor descriptor = new ObjectDescriptor();
        String name = "name";
        
        builder.addChild(descriptor, name, false);
        parent = builder.build();
        
        assertNotNull(parent);
        TreeNode[] children = parent.getChildren();
        assertNotNull(children);
        assertEquals(1, children.length);
        
        LabeledTreeNode child = (LabeledTreeNode) children[0];
        assertEquals(descriptor, child.getValue());
        assertEquals(descriptor, child.getValue());
        assertEquals(parent, child.getParent());
        assertEquals(name, child.getLabel());
    }
    
    public void testAddNullChild() throws Exception {
        String name = "";
        builder.addChild(null, name);
        parent = builder.build();
        
        TreeNode[] children = parent.getChildren();
        assertNotNull(children);
        assertEquals(1, children.length);
        
        LabeledTreeNode child = (LabeledTreeNode) children[0];
        assertNull(child.getValue());
        assertEquals(name, child.getLabel());
        assertEquals(parent, child.getParent());
    }
    
    public void testDoesNotAddNullChild() throws Exception {
        builder.addChild(null, null, false);
        parent = builder.build();
        
        assertNull(parent.getChildren());
    }
    
    public void testPreservesChildren() throws Exception {
        LabeledTreeNode child = new LabeledTreeNode(null);
        parent.setChildren(new TreeNode[] {child});
        builder.addChild(null, "");
        
        parent = builder.build();
        TreeNode[] children = parent.getChildren();
        assertNotNull(children);
        assertEquals(2, children.length);
        assertEquals(child, children[0]);
        assertNull(children[1].getValue());
    }
    
    public void testAddTagChild() throws Exception {
        builder.addTagChild(new Tag(Tags.NAME, ""), "label", false);
        builder.addTagChild(new Tag("", ""), "label", true);
        parent = builder.build();
        
        TreeNode[] children = parent.getChildren();
        assertNotNull(children);
        assertEquals(2, children.length);
        
        TreeNode child = children[0];
        assertTrue(child instanceof LabeledTagTreeNode);
        assertFalse(((LabeledTagTreeNode) child).isEditable());
        child = children[1];
        assertTrue(((LabeledTagTreeNode) child).isEditable());
    }
}